import request from '@/utils/request';
<template>
  <div>
    <el-dialog
      title="创建门店管理账号"
      :visible.sync="visible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="登录名称" prop="userName" :rules="{ required: true, message: '登录名称不能为空'}" label-width="120px">
          <div style="width: 70%;">
            <el-input v-model="form.userName" placeholder="请输入登录名称" clearable size="small" />
          </div>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName" :rules="{ required: true, message: '用户昵称不能为空'}" label-width="120px">
          <div style="width: 70%;">
            <el-input v-model="form.nickName" placeholder="请输入用户昵称" clearable size="small" />
          </div>
        </el-form-item>
        <el-form-item label="手机号" prop="phonenumber" :rules="{ required: true, message: '手机号不能为空'}" label-width="120px">
          <div style="width: 70%;">
            <el-input v-model="form.phonenumber" placeholder="请输入手机号" clearable size="small" />
          </div>
        </el-form-item>
        <el-form-item label="邮箱号" prop="email" :rules="{ required: true, message: '邮箱号不能为空'}" label-width="120px">
          <div style="width: 70%;">
            <el-input v-model="form.email" placeholder="请输入邮箱号" clearable size="small" />
          </div>
        </el-form-item>
        <el-form-item label="登录密码" prop="password" :rules="{ required: true, message: '登录密码不能为空'}" label-width="120px">
          <div style="width: 70%;">
            <el-input v-model="form.password" placeholder="请输入登录密码" clearable size="small" />
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">{{ form.id ? '立即修改' : '立即创建' }}</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: Number
  },
  data() {
    return {
      form: {}
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.getList()
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    getList() {
      const { id } = this.$props
      request({ url: 'system/company/adminList', method: 'get', params: { pageNum: 1, pageSize: 1, companyId: id }}).then(res => {
        console.log(res)
      })
    },
    onSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          // 拆分城市
          this.request({
            ...this.form,
            companyId: this.$props.id
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async request(params) {
      try {
        await request({
          url: 'system/company/addCompanyAdmin',
          method: 'post',
          data: params
        })
        this.$message({
          message: '创建成功',
          type: 'success',
          duration: 2000,
          onClose: () => {
            this.$emit('add-success')
            this.$refs['form'].resetFields()
          }
        })
      } catch (error) {
        this.$message({
          message: error,
          type: 'warning'
        })
      }
      //
    }
  }
}
</script>
